package service

import (
	"blog/utils"
	"fmt"
	"github.com/xuri/excelize/v2"
	"strconv"
	"time"
)

// OpeExcelData 账单统计计算
func OpeExcelData() {

	var contentData [][]string
	headerData := []string{"名字", "年龄"}
	contentItem := []string{"张三", "19"}

	contentData = append(contentData, contentItem)

	for i := 0; i < 10; i++ {
		rangeAge := utils.GetRandNum(5, 60)
		contentData = append(contentData, []string{
			utils.GetRandName(1, true), strconv.Itoa(rangeAge),
		})
	}
	//fmt.Println(contentData)
	targetExcelName := "runtime/tmp.xlsx"
	targetShellName := "anxiong"
	WriteDataToExcel(headerData, contentData, targetExcelName, targetShellName)
}

func ReadExcelData() {
	targetExcelName := "runtime/k.xlsx"
	targetShellName := "anxiong"
	f, err := excelize.OpenFile(targetExcelName)
	if err != nil {
		println(err.Error())
		return
	}

	// 获取 Sheet1 上所有单元格
	rows, err := f.GetRows(targetShellName)
	// 这里会遍历所有的Excel文档
	dataList := make(map[string]float64)
	var month string
	var amout float64
	for i, v := range rows {
		if i == 0 { // 跳过第一行表头
			continue
		}
		for ki, kv := range v {
			// 如果数据为空就跳过处理
			fmt.Println(ki, kv)
			if kv == "" {
				fmt.Println("数据为空,跳出当前循环", ki)
				break
			} else {
				// 这里开始解析处理数据 格式化日期参数
				if ki == 0 {
					fmt.Println("=================================== ")
					fmt.Println("0==处理", kv)
					theTime, err := time.Parse("2006-1-2", kv)
					if err != nil {
						fmt.Println("转换错误", err)
					}
					month = theTime.Format("200601")
				}
				// 处理金额信息
				if ki == 2 {
					fmt.Println("2==显示金额信息", kv)
					currentMoney, _ := strconv.ParseFloat(kv, 64)
					fmt.Println("计算金额相加", currentMoney, amout)
					fmt.Println("2==累加金额", amout, "month=", month)

					if _, ok := dataList[month]; ok {
						// key 存在,就进行累加操作
						fmt.Println("key 存在,就进行累加操作")
						amout = amout + currentMoney
						dataList[month] = amout
						//fmt.Println("2==计算结果", dataList)
					} else {
						// key 不存在,表示新开启一个循环累加,需要重置统计汇总
						fmt.Println("key 不存在,表示新开启一个循环累加,需要重置统计汇总")
						amout = currentMoney
						dataList[month] = amout
					}

				}
			}
		}

	}
	fmt.Println("显示", dataList, month, amout)
}

// 写入数据

var CellCols = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}

func WriteDataToExcel(headerData []string, contentData [][]string, targetExcelName string, targetShellName string) {
	f := excelize.NewFile()
	// 设置插入数据的 sheet 名称
	index, _ := f.NewSheet(targetShellName)
	// 设置工作簿的默认工作表
	f.SetActiveSheet(index)
	headNumber := 1 // 列从1列开始
	for i, v := range headerData {
		cellName := fmt.Sprintf("%s%d", CellCols[i], headNumber)
		//fmt.Println(cellName)
		// 写入表头
		fmt.Println("写入表头", targetShellName, cellName, v)
		err := f.SetCellValue(targetShellName, cellName, v)
		if err != nil {
			fmt.Println("写入表头失败", err)
			return
		}
	}
	//写入内容
	lineNumber := 2 // 行 从2行开始
	for i, v := range contentData {
		//fmt.Println(cellName)
		for ki, kv := range v {
			// 这里需要操作 A2 B2 - A3 B3  ...
			//fmt.Println("查看 ki=", ki, "headNumber=", headNumber)
			cellName := fmt.Sprintf("%s%d", CellCols[ki], lineNumber+i)
			fmt.Println("写入内容", targetShellName, cellName, kv)
			err := f.SetCellValue(targetShellName, cellName, kv)
			if err != nil {
				fmt.Println("写入内容失败", err)
				return
			}
		}
	}
	// 根据指定路径保存文件
	if err := f.SaveAs(targetExcelName); err != nil {
		println(err.Error())
	}
}
